package com.rickpan.dto.response;

import com.rickpan.entity.TeamInviteCode;
import com.rickpan.entity.TeamMember;
import io.swagger.v3.oas.annotations.media.Schema;

import java.time.LocalDateTime;

/**
 * 团队邀请码响应DTO
 */
@Schema(description = "团队邀请码信息响应")
public class TeamInviteCodeResponse {

    @Schema(description = "邀请码ID", example = "1")
    private Long id;

    @Schema(description = "团队ID", example = "1")
    private Long teamId;

    @Schema(description = "邀请码", example = "ABC12345")
    private String inviteCode;

    @Schema(description = "创建者ID", example = "1")
    private Long creatorId;

    @Schema(description = "创建者用户名", example = "admin")
    private String creatorUsername;

    @Schema(description = "邀请角色", example = "MEMBER", allowableValues = {"ADMIN", "MEMBER"})
    private TeamMember.Role inviteRole;

    @Schema(description = "最大使用次数", example = "10")
    private Integer maxUses;

    @Schema(description = "已使用次数", example = "3")
    private Integer usedCount;

    @Schema(description = "过期时间", example = "2025-01-18T10:00:00")
    private LocalDateTime expireTime;

    @Schema(description = "状态", example = "ACTIVE", allowableValues = {"ACTIVE", "EXPIRED", "DISABLED", "EXHAUSTED"})
    private TeamInviteCode.Status status;

    @Schema(description = "创建时间", example = "2025-01-11T10:00:00")
    private LocalDateTime createdAt;

    @Schema(description = "更新时间", example = "2025-01-11T10:00:00")
    private LocalDateTime updatedAt;

    @Schema(description = "是否有效", example = "true")
    private Boolean isValid;

    @Schema(description = "剩余使用次数", example = "7")
    private Integer remainingUses;

    // 构造函数
    public TeamInviteCodeResponse() {}

    public TeamInviteCodeResponse(TeamInviteCode inviteCode) {
        this.id = inviteCode.getId();
        this.teamId = inviteCode.getTeamId();
        this.inviteCode = inviteCode.getInviteCode();
        this.creatorId = inviteCode.getCreatorId();
        this.inviteRole = inviteCode.getInviteRole();
        this.maxUses = inviteCode.getMaxUses();
        this.usedCount = inviteCode.getUsedCount();
        this.expireTime = inviteCode.getExpireTime();
        this.status = inviteCode.getStatus();
        this.createdAt = inviteCode.getCreatedAt();
        this.updatedAt = inviteCode.getUpdatedAt();
        this.isValid = inviteCode.isValid();
        this.remainingUses = calculateRemainingUses(inviteCode);
    }

    // 静态工厂方法
    public static TeamInviteCodeResponse from(TeamInviteCode inviteCode) {
        return new TeamInviteCodeResponse(inviteCode);
    }

    // 计算剩余使用次数
    private Integer calculateRemainingUses(TeamInviteCode inviteCode) {
        if (inviteCode.getMaxUses() == null) {
            return null; // 无限制
        }
        return Math.max(0, inviteCode.getMaxUses() - inviteCode.getUsedCount());
    }

    // Getter和Setter方法
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Long getTeamId() {
        return teamId;
    }

    public void setTeamId(Long teamId) {
        this.teamId = teamId;
    }

    public String getInviteCode() {
        return inviteCode;
    }

    public void setInviteCode(String inviteCode) {
        this.inviteCode = inviteCode;
    }

    public Long getCreatorId() {
        return creatorId;
    }

    public void setCreatorId(Long creatorId) {
        this.creatorId = creatorId;
    }

    public String getCreatorUsername() {
        return creatorUsername;
    }

    public void setCreatorUsername(String creatorUsername) {
        this.creatorUsername = creatorUsername;
    }

    public TeamMember.Role getInviteRole() {
        return inviteRole;
    }

    public void setInviteRole(TeamMember.Role inviteRole) {
        this.inviteRole = inviteRole;
    }

    public Integer getMaxUses() {
        return maxUses;
    }

    public void setMaxUses(Integer maxUses) {
        this.maxUses = maxUses;
    }

    public Integer getUsedCount() {
        return usedCount;
    }

    public void setUsedCount(Integer usedCount) {
        this.usedCount = usedCount;
    }

    public LocalDateTime getExpireTime() {
        return expireTime;
    }

    public void setExpireTime(LocalDateTime expireTime) {
        this.expireTime = expireTime;
    }

    public TeamInviteCode.Status getStatus() {
        return status;
    }

    public void setStatus(TeamInviteCode.Status status) {
        this.status = status;
    }

    public LocalDateTime getCreatedAt() {
        return createdAt;
    }

    public void setCreatedAt(LocalDateTime createdAt) {
        this.createdAt = createdAt;
    }

    public LocalDateTime getUpdatedAt() {
        return updatedAt;
    }

    public void setUpdatedAt(LocalDateTime updatedAt) {
        this.updatedAt = updatedAt;
    }

    public Boolean getIsValid() {
        return isValid;
    }

    public void setIsValid(Boolean isValid) {
        this.isValid = isValid;
    }

    public Integer getRemainingUses() {
        return remainingUses;
    }

    public void setRemainingUses(Integer remainingUses) {
        this.remainingUses = remainingUses;
    }

    @Override
    public String toString() {
        return "TeamInviteCodeResponse{" +
                "id=" + id +
                ", teamId=" + teamId +
                ", inviteCode='" + inviteCode + '\'' +
                ", creatorId=" + creatorId +
                ", creatorUsername='" + creatorUsername + '\'' +
                ", inviteRole=" + inviteRole +
                ", maxUses=" + maxUses +
                ", usedCount=" + usedCount +
                ", expireTime=" + expireTime +
                ", status=" + status +
                ", createdAt=" + createdAt +
                ", updatedAt=" + updatedAt +
                ", isValid=" + isValid +
                ", remainingUses=" + remainingUses +
                '}';
    }
}
